package com.movitech.mobile.cz.base.sys.controller;


import com.movitech.mobile.cz.base.modules.controller.RestReturnResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.movitech.mobile.cz.base.common.util.CommonConstants;
import com.movitech.mobile.cz.base.sys.bizvo.MessageTemplateListRequestVO;
import com.movitech.mobile.cz.base.sys.bizvo.MessageTemplateRequestVO;
import com.movitech.mobile.cz.base.sys.entity.SMessageTemplate;
import com.movitech.mobile.cz.base.sys.service.SMessageTemplateService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
/**
 * <p>
 * SMessageTemplate 前端控制器
 * </p>
 *
 * @author Movitech Mobile
 * @since 2018-02-28
 */
@RestController
@RequestMapping("/v1/sMessageTemplate")
@Api(value = "消息模板基础操作接口", description = "消息模板基础操作接口")
public class SMessageTemplateController {
    @Autowired
    @Qualifier("sMessageTemplateService")
    private SMessageTemplateService sMessageTemplateService;

    @GetMapping("/allList")
    @ApiOperation(
            value = "获取所有消息模板接口",
            notes = "获取所有消息模板接口",
            response = SMessageTemplate.class
    )
    public RestReturnResult list() {
        RestReturnResult restReturnResult = RestReturnResult.success();
        Page<SMessageTemplate> page = sMessageTemplateService.selectPage(new Page<SMessageTemplate>());
        restReturnResult.setData(page);
        return restReturnResult;
    }

    @PostMapping("/searchList/{pageNumber}")
    @ApiOperation(
            value = "消息模板列表查询接口",
            notes = "消息模板列表查询接口",
            response = SMessageTemplate.class
    )
    public RestReturnResult searchList(@RequestBody SMessageTemplate sMessageTemplate,
                @ApiParam(value = "分页页数")@PathVariable Integer pageNumber) {
        RestReturnResult restReturnResult = RestReturnResult.success();
        Page<SMessageTemplate> page = new Page<>();
        page.setCurrent(pageNumber);
        EntityWrapper wrapper = new EntityWrapper<>();
        wrapper.setEntity(sMessageTemplate);
        page = sMessageTemplateService.selectPage(page,wrapper);
        restReturnResult.setData(page);
        return restReturnResult;
    }

    @GetMapping("/getSMessageTemplate/{id}")
    @ApiOperation(
            value = "消息模板对象ID获取接口",
            notes = "消息模板对对象ID获取接口",
            response = SMessageTemplate.class
    )
    public RestReturnResult addSMessageTemplate(@PathVariable String id) {
        RestReturnResult restReturnResult = RestReturnResult.success();
        SMessageTemplate sMessageTemplate = sMessageTemplateService.selectById(id);
        restReturnResult.setData(sMessageTemplate);
        return restReturnResult;
    }

    @PostMapping("/add")
    @ApiOperation(
            value = "消息模板添加接口",
            notes = "消息模板添加接口",
            response = SMessageTemplate.class
    )
    public RestReturnResult add(@RequestBody SMessageTemplate sMessageTemplate) {
        RestReturnResult restReturnResult = RestReturnResult.success();
        sMessageTemplateService.insert(sMessageTemplate);
        restReturnResult.setData(sMessageTemplate);
        return restReturnResult;
    }

    @PutMapping("/update")
    @ApiOperation(
            value = "消息模板更新接口",
            notes = "消息模板更新接口",
            response = SMessageTemplate.class
    )
    public RestReturnResult update(@RequestBody SMessageTemplate sMessageTemplate) {
        RestReturnResult restReturnResult = RestReturnResult.success();
        sMessageTemplateService.updateById(sMessageTemplate);
        restReturnResult.setData(sMessageTemplate);
        return restReturnResult;
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(
            value = "消息模板通过ID删除接口",
            notes = "消息模板通过ID删除接口",
            response = SMessageTemplate.class
    )
    public RestReturnResult update(@PathVariable String id) {
        RestReturnResult restReturnResult = RestReturnResult.success();
        sMessageTemplateService.deleteById(id);
        return restReturnResult;
    }

    /**
     * 消息模版列表 接口
     * @param 
     * @return
     */
    @ApiOperation(
            value = "消息模版列表 接口",
            notes = "消息模版列表 接口",
            response = SMessageTemplate.class
    )
    @RequestMapping(value = "/getMessageTemplateList", method = RequestMethod.POST)
    @ResponseBody
    public RestReturnResult<Object> getMessageTemplateList(@RequestBody MessageTemplateListRequestVO messageTemplateListRequestVO){
        RestReturnResult<Object> hResult = new RestReturnResult<Object>(CommonConstants.FAIL, CommonConstants.ERROR_CODES[0], CommonConstants.ERROR_DESCS[0]);

        try{
            Page<SMessageTemplate> page = new Page<>();
            page.setCurrent(messageTemplateListRequestVO.getPageNumber());
            SMessageTemplate sMessageTemplate = new SMessageTemplate();
            sMessageTemplate.setMsgType(messageTemplateListRequestVO.getMsgType());
            sMessageTemplate.setMsgStatus(messageTemplateListRequestVO.getMsgStatus());
            sMessageTemplate.setDelFlag(CommonConstants.NO_INTEGER);
            EntityWrapper wrapper = new EntityWrapper<>();
            wrapper.setEntity(sMessageTemplate);
//            wrapper.like("msg_name", messageTemplateListRequestVO.getMsgName());
            page = sMessageTemplateService.selectPage(page,wrapper);

            hResult = new RestReturnResult<Object>(CommonConstants.SUCCESS, page);
        }catch(Exception e){
            e.printStackTrace();
            hResult = new RestReturnResult<Object>(CommonConstants.FAIL, CommonConstants.ERROR_CODES[1], CommonConstants.ERROR_DESCS[1]);
        }

        return hResult;

    }

    /**
     * 消息模版 新增/修改 接口
     * @param messageTemplateRequestVO
     * @return
     */
    @ApiOperation(
            value = "消息模版 新增/修改 接口",
            notes = "消息模版 新增/修改 接口",
            response = SMessageTemplate.class
    )
    @RequestMapping(value = "/saveOrUpdateMessageTemplate", method = RequestMethod.POST)
    @ResponseBody
    public RestReturnResult<Object> saveOrUpdateMessageTemplate(@RequestBody MessageTemplateRequestVO messageTemplateRequestVO){
        RestReturnResult<Object> hResult = new RestReturnResult<Object>(CommonConstants.FAIL, CommonConstants.ERROR_CODES[0], CommonConstants.ERROR_DESCS[0]);

        try{
            hResult = sMessageTemplateService.saveOrUpdateMessageTemplate(messageTemplateRequestVO);
        }catch(Exception e){
            e.printStackTrace();
            hResult = new RestReturnResult<Object>(CommonConstants.FAIL, CommonConstants.ERROR_CODES[1], CommonConstants.ERROR_DESCS[1]);
        }

        return hResult;
    }

    /**
     * 消息模版 删除接口
     * @param id
     * @return
     */
    @ApiOperation(
            value = "消息模版 删除接口",
            notes = "消息模版 删除接口",
            response = SMessageTemplate.class
    )
    @RequestMapping(value = "/deleteMessageTemplate", method = RequestMethod.GET)
    @ResponseBody
    public RestReturnResult<Object> deleteMessageTemplate(String id){
        RestReturnResult<Object> hResult = new RestReturnResult<Object>(CommonConstants.FAIL, CommonConstants.ERROR_CODES[0], CommonConstants.ERROR_DESCS[0]);

        try{
            hResult = sMessageTemplateService.updateMessageTemplateForDel(id);
        }catch(Exception e){
            e.printStackTrace();
            hResult = new RestReturnResult<Object>(CommonConstants.FAIL, CommonConstants.ERROR_CODES[1], CommonConstants.ERROR_DESCS[1]);
        }

        return hResult;
    }

    /**
     * 消息模版 启用/停用  接口
     * @param messageTemplateRequestVO
     * @return
     */
    @ApiOperation(
            value = "消息模版 启用/停用  接口",
            notes = "消息模版 启用/停用  接口",
            response = SMessageTemplate.class
    )
    @RequestMapping(value = "/updateMessageTemplateStatus", method = RequestMethod.POST)
    @ResponseBody
    public RestReturnResult<Object> updateMessageTemplateStatus(@RequestBody MessageTemplateRequestVO messageTemplateRequestVO){
        RestReturnResult<Object> hResult = new RestReturnResult<Object>(CommonConstants.FAIL, CommonConstants.ERROR_CODES[0], CommonConstants.ERROR_DESCS[0]);

        try{
            hResult = sMessageTemplateService.updateMessageTemplateStatus(messageTemplateRequestVO);
        }catch(Exception e){
            e.printStackTrace();
            hResult = new RestReturnResult<Object>(CommonConstants.FAIL, CommonConstants.ERROR_CODES[1], CommonConstants.ERROR_DESCS[1]);
        }

        return hResult;
    }

}

